package cn.digiwin.wx.utils;

import cn.digiwin.wx.qyh.common.WeixinComm;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.ToolUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 微信通用调用处理类
 * 包括第三方接口调用配置地址
 * Created by soldier on 2017-9-7.
 */
public class WeixinHandler {

    /**
     * 获取微信cookie的当前认证用户员工编码
     * for 调用第三方分销接口参数使用
     *
     * @param request
     * @return
     */
    public static String getStaffNo(HttpServletRequest request) {
        String staffno = CookieUtils.getCookie(request, "_WX_ACCOUNT_");
        if (ToolUtil.isEmpty(staffno)) {
            staffno = "--";
        }
        return staffno;
    }

    // 给微信端发送消息
    public static void sendMsg(List<Dto> user, String msg) {
        try {
            if (user.size() > 0) {
                String ids_qyh = "";
                //List<String> ids_gzh = new ArrayList<>();
                for (Dto dto : user) {
                    String account_ = dto.getString("GWAA001");
                    if (ToolUtil.isNotEmpty(account_)) {
                        //if (dto.getString("GWAA010").equals("1")) {
                        ids_qyh += account_ + "|";
                        //} else {
                        //ids_gzh.add(account_);
                        //}
                    }
                }
                //发送到企业号
                if (ToolUtil.isNotEmpty(ids_qyh)) {
                    WeixinComm.sendMsg(ids_qyh, msg);
                }

                //发送到服务号
//                if (AppUtils.isNotEmpty(ids_gzh)) {
//                    WeixinGzhComm.sendMsg(ids_gzh, msg);
//                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 给微信端发送消息
    public static void sendMsg(Dto user, String msg) {
        List<Dto> userList = new ArrayList<>();
        userList.add(user);
        sendMsg(userList, msg);
    }


}
